/**
 * 
 */
package com.ginger.rabbitmq.exchange.api.qos;

import java.time.Duration;
import java.time.Instant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ginger.properties.RabbitMQProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Description: 生产者
 * @author 姜锋
 * @date 2019年3月29日 上午9:40:05
 * @version V1.0
 */
public class Producer {
	/** 日志对象 **/
	private final static Logger logger = LoggerFactory.getLogger(Producer.class);
	public static void main(String[] args) throws Exception {
		// 1.创建连接工厂
		ConnectionFactory connectionFactory = new ConnectionFactory();
		// 1.1设置连接信息
		connectionFactory.setHost(RabbitMQProperties.RABBITMQ_HOST);
		connectionFactory.setPort(RabbitMQProperties.RABBITMQ_PORT);
		connectionFactory.setVirtualHost(RabbitMQProperties.RABBITMQ_VIRTUAL_HOST);
		connectionFactory.setUsername(RabbitMQProperties.USER_NAME);
		connectionFactory.setPassword(RabbitMQProperties.PASSWORD);
		// 2.通过连接工厂创建连接
		Connection connection = connectionFactory.newConnection();
		// 3.通过connection创建
		Channel channel = connection.createChannel();
		// 4.声明
		String exchangeName = "test_qos_exchange";
		String routingKey = "qos.save";
		String message;
		Instant start = Instant.now();
		for (int i = 0; i < 10; i++) {
			message = "这是 "+(i+1)+"条生产的数据:  Hello RabbitMQ Send QOS message !";
			channel.basicPublish(exchangeName, routingKey, true, null, message.getBytes());

		}
		// 6.通过channel发送数据
		Instant end = Instant.now();
		logger.warn("计算发送耗时毫秒:" + Duration.between(start, end).getSeconds() + "秒");

		channel.close();
		connection.close();
	}
}
